#! usr/bin/perl -w
use strict;

### load database file
sub LOADdb{
	my $filedb=$_[0];
	my %db=();

	print "\nDATABASE FILE : $filedb   Loading...\n";
	open(FILEDB, $filedb) or die "cannot open $filedb";
	while(my $line=<FILEDB>)
	{
		chomp($line);
		$line=~ s/ +/ /gi;#separate rows with a whitespace only
		my($chr,$rsID ,$pos ,$a0, $a1, $freqref)=split(/ /,$line);
		
		$db{$rsID}{'chr'}=$chr;
		$db{$rsID}{'pos'}=$pos;
		$db{$rsID}{'a0'}=$a0;
		$db{$rsID}{'a1'}=$a1;
		$db{$rsID}{'freqref'}=$freqref;
	}
close FILEDB;
	return(\%db);
}

###################################################################

### load newbim files
sub LOADfile{
	my $filename=$_[0];
	my %f=();
	my @rslist=();
	
	print "\nFILE : $filename   Loading...\n";
	open(FILE, $filename) or die "cannot open $filename";
	while(my $line=<FILE>)
	{
		chomp($line);
		my($chrom,$snpID ,$genetD ,$position, $a0, $a1)=split(/\t/,$line);
		push(@rslist,$snpID); 

		$f{$snpID}{'chrom'}=$chrom;
		$f{$snpID}{'genetD'}=$genetD;
		$f{$snpID}{'position'}=$position;
		$f{$snpID}{'a0'}=$a0;
		$f{$snpID}{'a1'}=$a1;
	}
	close FILE;
	return(\%f,\@rslist);
}

####################################################################

### load keeprs_liftover_wellconverted files
sub LOADkeeprs{
	my $rsfile=$_[0];
	my %keeprsfile=();
	
	print "Loading : $rsfile \n";
	open(RSFILE, $rsfile) or die "cannot open $rsfile";
	while(my $line=<RSFILE>)
	{
		chomp($line);
		my($rsIDdata,$rsIDdb)=split(/ /,$line);

		$keeprsfile{$rsIDdata}{'rsIDdb'}=$rsIDdb;
	}
	close RSFILE;

	return(\%keeprsfile);
}

####################################################################

### load keepchrpos_liftover_wellconverted files
sub LOADkeepchrpos{
	my $chrposfile=$_[0];
	my %keepchrposfile=();
	
	print "Loading : $chrposfile \n";
	open(CHRPOSFILE, $chrposfile) or die "cannot open $chrposfile";
	while(my $line=<CHRPOSFILE>)
	{
		chomp($line);
		my($chrpos,$IDdata,$freqDBa0,$freqDATAa0,$deltafreq)=split(/\t/,$line);

		$keepchrposfile{$IDdata}{'chrpos'}=$chrpos;
		$keepchrposfile{$IDdata}{'freqDBa0'}=$freqDBa0;
		$keepchrposfile{$IDdata}{'freqDATAa0'}=$freqDATAa0;
		$keepchrposfile{$IDdata}{'deltafreq'}=$deltafreq;
	}
	close CHRPOSFILE;

	return(\%keepchrposfile);

}

####################################################################

### create newbimfile to impute
sub create_newfile{
	my($ref_file,$keeprs,$keepchrpos,$ref_db,$dataname,$ref_IDlist)=@_;
	my %f=%$ref_file;
	my %db=%$ref_db;
	my @IDlist=@$ref_IDlist;

	# load keeprs file
	my $ref_keeprsfile=&LOADkeeprs($keeprs);
	my %keeprsfile=%$ref_keeprsfile;
	
	# load keepchrpos file
	my $ref_keepchrposfile=&LOADkeepchrpos($keepchrpos);
	my %keepchrposfile=%$ref_keepchrposfile;	

	# write newbimfile to impute
	my $newbimfile="./bim_to_impute/newbim_to_impute_$dataname.bim";
	open(NEWBIM,">$newbimfile") or die "cannot open NEWBIMFILE: $newbimfile $!\n";

	foreach my $item (@IDlist){

		# check for rsID to match with database
		if(exists($keeprsfile{$item})){
			my $itemdb=$keeprsfile{$item}{'rsIDdb'};
			$f{$item}{'a0'}=$db{$itemdb}{'a0'};
			$f{$item}{'a1'}=$db{$itemdb}{'a1'};
			print NEWBIM "$f{$item}{'chrom'}\t$itemdb\t$f{$item}{'genetD'}\t$db{$itemdb}{'pos'}\t$f{$item}{'a0'}\t$f{$item}{'a1'}\n";
		}
		elsif(exists($keepchrposfile{$item})){
			my $itemdb=$keepchrposfile{$item}{'chrpos'};
			$f{$item}{'a0'}=$db{$itemdb}{'a0'};
			$f{$item}{'a1'}=$db{$itemdb}{'a1'};
			print NEWBIM "$f{$item}{'chrom'}\t$itemdb\t$f{$item}{'genetD'}\t$db{$itemdb}{'pos'}\t$f{$item}{'a0'}\t$f{$item}{'a1'}\n";
		}
		else{
			print NEWBIM "$f{$item}{'chrom'}\t$item\t$f{$item}{'genetD'}\t$f{$item}{'position'}\t$f{$item}{'a0'}\t$f{$item}{'a1'}\n";
		}
	}

	close(NEWBIM);
	print "\nsee $newbimfile\n\n";
}


####################################################################

sub main(){
	
	# collect arguments
	my $dbfilename=$ARGV[0];
	shift(@ARGV);
	my $datefile=$ARGV[0];
	shift(@ARGV);

	# load database file
	my $ref_db=&LOADdb($dbfilename);

	# create output directory if it doesn't exist yet 
	if(! -d "./bim_to_impute"){mkdir("./bim_to_impute", 0777);}

	foreach my $file ( @ARGV ){

		my $name;

		if(-f $file){
			my($ref_f,$ref_rslist)=&LOADfile($file);
		
			# extract data name
			if ($file =~ /newbimfile\_(.*)\.bim\_\d+\.out$/){
				$name=$1;
			}
			else { print "Error : inputfile must be newbimfile_dataname.bim_date.out \n \t with adujstable dataname and date. \n";}

			my $keeprsname="output_liftoversh/informations/keeprs_liftover_wellconverted_".$name."_".$datefile.".txt";
			my $keepchrposname="output_liftoversh/informations/keepchrpos_liftover_wellconverted_".$name."_".$datefile.".txt";

			# create newbimfile to impute
			&create_newfile($ref_f,$keeprsname,$keepchrposname,$ref_db,$name,$ref_rslist);
		}
		else{
			print "$file doesn't exist.\n";
		}
	}

exit;
}

main();

# example : perl create_bimfile_to_impute.pl ../../../db/NEW_1000/EUR.chrfulldata.db 07262011 ../newbimfile_*
